/* CategoryList.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the CategoryList, which stores a list of TaskLists, each representing
a different category of tasks. CategoryList provides functions for editing the list
and selecting different elements of it.
*/

#ifndef __MZ_CATEGORY_LIST_H__
#define __MZ_CATEGORY_LIST_H__

#include "_common/Noncopyable.h"
#include <list>
#include "MutableTaskList.h"

#include <string>
#include <istream>

using std::string;
using std::list;
using std::istream;



class CategoryList : private list<MutableTaskList>, public Noncopyable {
public:
	// Number of categories (i.e. number of separate TaskLists).
	int Count() const;
	// Name of the category with the given index.
	const string &Name(int index) const;
	
	// Index of the currently selected category.
	int Selection() const;
	
	// Get the given category.
	const TaskList &operator[](int index) const;
	// Get the currently selected category.
	const TaskList &Selected() const;


private:
	// Functions for the use of Editor alone.
	
	// Load all task lists in the given file.
	bool Load(const string &path);
	bool Load(istream &in);
	// Get the path this list was loaded from.
	const string &FilePath() const;
	// Save all task lists.
	void Save() const;
	// Revert to the saved version.
	void Revert();
	// Restore all deleted task lists.
	void Resurrect();
	// Select the given row.
	void Select(int row);
		
	// Switch categories.
	void SwitchToFirst();
	void SwitchToPrevious();
	void SwitchToNext();
	
	// Edit the categories list.
	void MoveUp();
	void MoveDown();
	void Add();
	void Delete();


private:
	// Load tasks from the given file and append them to the current list.
	bool Append(const string &path);
	bool Append(istream &in);

	// Helper function.
	iterator GetIterator(int index);
	const_iterator GetIterator(int index) const;
	
	
private:
	// Iterator to the current category.
	iterator category;
	
	// Path to the data file, and path to the file containing deleted categories
	// (in case they ever need to be restored).
	string filePath;
	string deletedPath;
	
	
	// Allow Editor access to the private functions.
	// The alternative to this would be to create a separate MutableCategoryList,
	// like I did with MutableTaskList.
	friend class Editor;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
